#!/bin/bash

TARGET_VALUE=30
CONFIG_FILE="/etc/sysctl.d/90-softlockup.conf"

# 验证目标值有效性
if ! [[ $TARGET_VALUE =~ ^[0-9]+$ ]] || [[ $TARGET_VALUE -lt 1 ]]; then
   echo "错误：阈值必须为正整数" >&2
   exit 2
fi

# 1. 检查当前值并更新临时设置
CURRENT_VAL=$(cat /proc/sys/kernel/watchdog_thresh 2>/dev/null)
if [[ $CURRENT_VAL == "$TARGET_VALUE" ]]; then
   echo "当前阈值已为${TARGET_VALUE}秒，无需修改"
else
   echo -n "更新阈值: ${CURRENT_VAL:-未知} -> ${TARGET_VALUE}..."
   if echo "$TARGET_VALUE" > /proc/sys/kernel/watchdog_thresh 2>/dev/null; then
      echo "成功(临时生效)"
   else
      echo -e "\n错误：无法修改临时设置" >&2
      exit 3
   fi
fi

# 2. 最终验证
if [[ $(sysctl -n kernel.watchdog_thresh) -eq $TARGET_VALUE ]]; then
   echo -e "\n状态：软死锁阈值已成功设置为${TARGET_VALUE}秒"
   exit 0
else
   echo -e "\n警告：最终验证失败，请手动检查" >&2
   exit 5
fi